// pages/grade/grade.js

const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    multiArray: [
      ['2018-2019', '2017-2018', '2016-2017', '2015-2016', '2014-2015', '2013-2014', '2012-2013', '2011-2012', '2010-2011'],
      ['第一学期', '第二学期'],
    ],
    objectMultiArray: [
      [{
          id: 0,
          name: '2018-2019'
        },
        {
          id: 1,
          name: '2017-2018'
        },
        {
          id: 2,
          name: '2016-2017'
        },
        {
          id: 3,
          name: '2015-2016'
        },
        {
          id: 4,
          name: '2014-2015'
        },
        {
          id: 5,
          name: '2013-2014'
        },
        {
          id: 6,
          name: '2012-2013'
        },
        {
          id: 7,
          name: '2011-2012'
        },
        {
          id: 8,
          name: '2010-2011'
        },
      ],
      [{
          id: 0,
          name: '第一学期'
        },
        {
          id: 1,
          name: '第二学期'
        },
      ],
    ],
    multiIndex: [0, 0],
    xh: "",
    mm: "",
    cookie: "",
    grades: [],
    fId: '',


    data0: '',
    data1: [],
    data2: '',
    data3: '',
    data4: '',

    arr: [],

    courseCount: '',
    pass: '',


    isPoint: false,
    titleText: '成绩',
  },

  bindMultiPickerChange: function(e) {

    wx.showLoading({
      title: '加载中',
    })

    let that = this;

    let xnIndex = e.detail.value[0];
    let xqIndex = e.detail.value[1];

    this.setData({
      multiIndex: e.detail.value
    });

    let xnArg = this.data.multiArray[0][xnIndex];
    let xqArg = xqIndex + 1;


    wx.request({
      url: app.globalData.apiAccessSuffix + '/query/grade',
      data: {
        xh: that.data.xh,
        mm: that.data.mm,
        fId: that.data.fId,
        cookie: that.data.cookie,
        xnArg: xnArg,
        xqArg: xqArg
      },
      dataType: 'json',
      success: function(res) {
        let obj = res.data;
        console.log(obj);
        that.setData({
          grades: obj
        })
        if (obj.length <= 0) {
          wx.showToast({
            title: '该学期暂无课程',
            icon: 'success',
            duration: 2000
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },

  queryPoint: function() {
    let that = this;
    wx.request({
      url: app.globalData.apiAccessSuffix + '/query/grade',
      data: {
        xh: that.data.xh,
        mm: that.data.mm,
        fId: that.data.fId,
        cookie: that.data.cookie,
      },
      dataType: 'json',
      success: function(res) {
        let obj = res.data;
        console.log(obj);
        that.setData({
          data0: obj.data0,
          data1: obj.data1,
          data2: obj.data2,
          data3: obj.data3,
          data4: obj.data4,
          courseCount: obj.data1.length
        });
        let temp = obj.data1;
        console.log(temp);
        console.log(typeof temp);
        
        let jsonArr = [];
        for (let i = 0; i < temp.length; i++) {
          jsonArr[i] = temp[i];
        }
        that.setData({
          arr: jsonArr
        });
        if (obj.data3 > 2) {
          that.setData({
            pass: true
          });
        } else {
          that.setData({
            pass: false
          });
        }
      },
      complete: function() {
        wx.hideLoading();
      }
    });
  },

  showGradeInfo: function(e) {
    let data = e.currentTarget.dataset;
    wx.showModal({
      title: data.d0,
      content: "\r\n课程性质:  " + data.d1 +
        "\r\n课程归属:  " + data.d2 +
        "\r\n学       分:  " + data.d3 +
        "\r\n期中成绩:  " + data.d5 +
        "\r\n期末成绩:  " + data.d6 +
        "\r\n实验成绩:  " + data.d7 +
        "\r\n成       绩:  " + data.d8 +
        "\r\n补考成绩:  " + data.d9 +
        "\r\n是否重修:  " + data.d10 +
        "\r\n开课学院:  " + data.d11 +
        "\r\n备       注:  " + data.d12 +
        "\r\n补考备注:  " + data.d13,
      cancelText: '关闭',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },
  goGradePointInfo: function(e) {
    let data = e.currentTarget.dataset;
    let calText = '';
    if (!data.d14) {
      calText = '否';
    }
    wx.showModal({
      title: data.d0,
      content: 
        "\r\n课程编号:  " + data.d16 +
        "\r\n课程性质:  " + data.d1 +
        "\r\n课程归属:  " + data.d2 +
        "\r\n课程学年:  " + data.d17 +
        "\r\n课程学期:  第" + data.d18 + "学期" +
        "\r\n学分:  " + data.d3 +
        "\r\n平时成绩:  " + data.d4 +
        "\r\n期中成绩:  " + data.d5 +
        "\r\n期末成绩:  " + data.d6 +
        "\r\n实验成绩:  " + data.d7 +
        "\r\n成绩:  " + data.d8 +
        "\r\n补考成绩:  " + data.d9 +
        "\r\n是否重修:  " + data.d10 +
        "\r\n开课学院:  " + data.d11 +
        "\r\n备注:  " + data.d12 +
        "\r\n补考备注:  " + data.d13 +
        "\r\n是否计入绩点计算:  " + calText +
        "\r\n获得绩点:  " + data.d15,
        cancelText: '关闭',
      success(res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let ffid = options.fid;
    let flag = false;
    if (ffid === '103') {
      wx.showLoading({
        title: '正在查询...',
      });
      flag = true;
      this.setData({
        titleText: '绩点'
      });
    }
    this.setData({
      xh: options.xh,
      mm: options.mm,
      cookie: decodeURIComponent(options.cookie),
      fId: ffid,
      isPoint: flag,
    });
    if(ffid === '103') {
      this.queryPoint();            
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})